package com.sali.Tree;

/**
 * @author sali
 * @date 2025/5/19 20:13
 */
public class JZ_79 {


    boolean isBalanced = true;

    public boolean IsBalanced_Solution (TreeNode pRoot) {
        // write code here
        if ( pRoot == null ) {
            return true;
        }

        getRes(pRoot);

        return isBalanced;
    }

    private int getRes(TreeNode curNode) {
        if ( curNode == null ) {
            return 0;
        }

        int leftHeight = getRes(curNode.left);
        int rightHeight = getRes(curNode.right);

        if ( Math.abs(leftHeight - rightHeight) > 1 ) {
            isBalanced = false;
        }

        return Math.max(leftHeight, rightHeight) + 1;
    }

}
